package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: redis
 * @BelongsPackage: com.example.demo.controller
 * @Author: HUI
 * @CreateTime: 2022-03-22 10:57
 * @Description:
 */

@Controller
public class MyController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping("/test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {

        System.out.println(redisTemplate);
        redisTemplate.opsForValue().set("luo","hui");
        

        List<String> list = (List<String>) request.getSession().getAttribute("list");
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add("xxxx");
        request.getSession().setAttribute("list", list);//每次session变化都要同步session

        response.getWriter().println("size: " + list.size());
        response.getWriter().println("sessionid: " + request.getSession().getId());


    }
}